#include "setting.h"
#include <QDebug>
#include <QSettings>
#include <QStandardPaths>
#include <QMessageBox>
#include <QErrorMessage>
#include <QApplication>
#include <QGraphicsEffect>
#include "unistd.h"
#include "customstyle.h"
#include "util/xatom-helper.h"
#include <kylin_system/window_management.hpp>
#include <kylin_system/user_manual.hpp>

#define MAINWINDOWTITLEBARHEIGHT 42
/***********#11358 【cpm】【KelvinV-W5821-LV--比亚迪精密】使用相机拍照片后，
 * *******发现系统将照片（jpg格式）自动归类到视频文件夹分类，图片文件夹找不到照片。******/
//#define SETTINGPAGEWIDTH 550
//#define SETTINGPAGEHEIGHT 400
/***********#11358 【cpm】【KelvinV-W5821-LV--比亚迪精密】使用相机拍照片后，
 * *******发现系统将照片（jpg格式）自动归类到视频文件夹分类，图片文件夹找不到照片。******/
#define SETTINGPAGEWIDTH 550
#define SETTINGPAGEHEIGHT 350
#define SETTINGPAGEWIDTHMAX 640
#define SETTINGPAGEHEIGHTMAX 400
#define ABOUTWINDOWWIDETH  420                 //窗口宽度
#define ABOUTWINDOWHEIGHT  316                 //窗口高度
#define ABOUTTITLEHEIGHT 42                    //标题栏高度

Setting::Setting(QWidget *parent)
  : QFrame(parent)
{
    //菜单及菜单项的设置
    menu = new QMenu;
    set = new QAction(tr("set"),menu);
    theme = new QAction(tr("theme"),menu);
    help = new QAction(tr("help"),menu);
    about = new QAction(tr("about"),menu);
    quit = new QAction(tr("quit"),menu);
    menu->setCursor(Qt::PointingHandCursor);
    menu->addAction(set);
    menu->addAction(help);
    menu->addAction(about);
    menu->addAction(quit);
    //“设置”菜单项对应的页面

/*****************暂时未启用的功能start***********************/
    delay_action = new QWidgetAction(this);
    delaylab = new QLabel;
    delay = new QWidget;
    delaylab->setText(tr("Delayed"));
    delaybtn = new SwitchButton(this);
    themeMenu = new QMenu;
    delayLayout = new QHBoxLayout;
    delay->setFixedHeight(30);
    //设置按钮布局
    delayLayout->addSpacing(12);
    delayLayout->addWidget(delaylab);
    delayLayout->addStretch(5);
    delayLayout->addWidget(delaybtn);
    delayLayout->addSpacing(10);
    delayLayout->setMargin(0);
    delay->setLayout(delayLayout);
    delay_action->setDefaultWidget(delay);

    QActionGroup *themeMenuGroup = new QActionGroup(this);
    QAction *autoTheme = new QAction(tr("Auto"),this);
    themeMenuGroup->addAction(autoTheme);
    themeMenu->addAction(autoTheme);
    autoTheme->setCheckable(true);
    QAction *lightTheme = new QAction(tr("Light"),this);
    themeMenuGroup->addAction(lightTheme);
    themeMenu->addAction(lightTheme);
    lightTheme->setCheckable(true);
    QAction *darkTheme = new QAction(tr("Dark"),this);
    themeMenuGroup->addAction(darkTheme);
    themeMenu->addAction(darkTheme);
    darkTheme->setCheckable(true);
    QList<QAction* > themeActions;
    themeActions<<autoTheme<<lightTheme<<darkTheme;
/*****************暂时未启用的功能end***********************/
    kdk::kabase::WindowManagement::setWindowMotifHint(this->winId());

    connect(menu,&QMenu::triggered,this,&Setting::triggerMenu);
    this->setForegroundRole(QPalette::Highlight);
    this->setAutoFillBackground(true);
    this->setStyleSheet("QWidget#widgetMain{border: 1px solid #FF00FF; border-radius: 5px;}");
    QGraphicsEffect *effect = nullptr;
    this->setFrameStyle(QFrame::Panel);
    this->setGraphicsEffect(effect);
}
Setting::~Setting(){
    delete menu;
    delete setPage;

}
void Setting::userManualTest(void)
{
    kdk::kabase::UserManualManagement userManualTest;
    if (!userManualTest.callUserManual(kdk::kabase::AppName::KylinCamera)) {
        qCritical() << "user manual call fail!";
    }

    return;
}

//点击menu按钮 下拉菜单
void Setting::triggerMenu(QAction *action){
    Q_EMIT settingTestSig();
    QString str = action->text();
     if(str == tr("help")){
         userManualTest();
     }
    if(str == tr("about")){
        QString aboutTitleName = tr("kylin-camera");
        QString appVersion = APP_VERSION;
        aboutWinWidget = new kdk::KAboutDialog(this,QIcon::fromTheme("kylin-camera"),tr(aboutTitleName.toLocal8Bit()),tr("Version: ") + appVersion);
        aboutWinWidget->setBodyText(tr("Camera is a very strong camera software. "
                                       "It has the characteristics of easy to use. "
                                       "You can realize the function of taking a photo recording and displaying the album"));
        aboutWinWidget->setBodyTextVisiable(false);
        aboutWinWidget->setWindowModality(Qt::WindowModal);
        aboutWinWidget->setWindowModality(Qt::ApplicationModal);
        kdk::kabase::WindowManagement::setWindowMotifHint(aboutWinWidget->winId());
        aboutWinWidget->show();
        aboutWinWidget->update();
        aboutWinWidget->exec();
    }
    if(str == tr("set")){
        setPage = new SettingPage();
        QString locale = QLocale::system().name();
        if(locale == "bo_CN"){
            QPoint pos = QWidget::mapToGlobal(QPoint((CurrentDeviceInfo::winWidth - SETTINGPAGEWIDTHMAX)/2,
                                                     (CurrentDeviceInfo::winHeight + MAINWINDOWTITLEBARHEIGHT - SETTINGPAGEHEIGHTMAX)/2));
            setPage->Geometry(&pos);
            setPage->setMinimumSize(SETTINGPAGEWIDTHMAX,SETTINGPAGEHEIGHTMAX);
            setPage->setMaximumSize(SETTINGPAGEWIDTHMAX,SETTINGPAGEHEIGHTMAX);
        }else{
            QPoint pos = QWidget::mapToGlobal(QPoint((CurrentDeviceInfo::winWidth - SETTINGPAGEWIDTH)/2,
                                                     (CurrentDeviceInfo::winHeight + MAINWINDOWTITLEBARHEIGHT - SETTINGPAGEHEIGHT)/2));
            setPage->Geometry(&pos);
            setPage->setMinimumSize(SETTINGPAGEWIDTH,SETTINGPAGEHEIGHT);
            setPage->setMaximumSize(SETTINGPAGEWIDTH,SETTINGPAGEHEIGHT);
        }

        setPage->setWindowModality(Qt::ApplicationModal);
        setPage->setWindowFlag(Qt::Tool);  //如果on为true，则在此小部件上设置窗口标志；否则清除标志
        kdk::kabase::WindowManagement::setWindowMotifHint(setPage->winId());
        setPage->show();
    }
    if(str == tr("quit")){
        this->window()->close();
    }
}


